package com.liuhong.shoping.controller;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuhong.shoping.common.ResponseMessage;
import com.liuhong.shoping.dao.OrgInfoMapper;
import com.liuhong.shoping.entity.OrgInfo;
import com.liuhong.shoping.entity.User;
import com.liuhong.shoping.entity.UserInfo;
import com.liuhong.shoping.service.IOrgInfoService;
import com.liuhong.shoping.vo.out.LoginBean;
import com.liuhong.shoping.vo.out.ResultBean;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhong
 * @since 2021-01-11
 */
@RestController
@RequestMapping("/orgInfo")
public class OrgInfoController {
	@Autowired
	private OrgInfoMapper orgInfoMapper;
	
	/**
	 * 
	 * 获取组织机构列表
	 */
	@PostMapping("getOrgList")
    public ResponseMessage getOrgList() 
	{
		try {
			 List<OrgInfo> orgList = orgInfoMapper.selectList(null);
			 return ResponseMessage.success(orgList);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * 
	 * @param { OrgInfo }
	 * 删除组织机构 
	 * 以及包含的的子集
	 */
	@Transactional
	@PostMapping("deleteOrgInfo")
    public ResponseMessage deleteOrgInfo(@RequestBody OrgInfo org) 
	{
		try {
			 int deleteRows = orgInfoMapper.deleteById(org.getId());
			 QueryWrapper<OrgInfo> queryWrapperOrgInfo = new QueryWrapper<>();
			 queryWrapperOrgInfo
			 	.lambda()
			 	.eq(OrgInfo::getParentsId, org.getId())
			 	.isNotNull(OrgInfo::getParentsId);
			 orgInfoMapper.delete(queryWrapperOrgInfo);
			 return ResponseMessage.success(deleteRows,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * 
	 * @param { OrgInfo }
	 * 修改组织机构
	 */
	@PostMapping("updateOrgInfo")
    public ResponseMessage updateOrgInfo(@RequestBody OrgInfo org) 
	{
		try {
			 int updateRows = orgInfoMapper.updateById(org);
			 return ResponseMessage.success(updateRows, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ UserInfo entity }
	 * 添加组织
	 */
	@PostMapping("addOrgNode")
    public ResponseMessage addOrgNode(@RequestBody OrgInfo org) 
	{
		try {
			org.setId(UUID.randomUUID().toString());
			org.setCreatedTime(LocalDateTime.now());
			int insertRows = orgInfoMapper.insert(org);
			return ResponseMessage.success(insertRows,"插入成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	
}

